/**
 * Created by Pyj on 2019/08/20.
 */
/***********************面板-start****************************/
var rowData=[];

//主界面参数面板
var filesGridOpt = {
    id: "filesGrid",//渲染对象ID 控件的唯一标识
    //title:"测试wdfGrid",//表格标题名暂时不要使用该属性,因为该实现为完善
    autoWidth: true,//是否自适应宽度,该属性设置时,列的width属性将失效,要让列的width属性生效请关闭该属性并设置colFixed属性为true
    autoHeight: true,
    //colFixed:true,//固定列宽,该属性启动时列width属性有效,autoWidth属性无效
    className: "test",//表格样式类名
    checkbox:false,//是否显示选中按钮
    isMulitCheck: false,//是否支持多选
    loadMsg: "正在加载数据请稍等",//表格加载显示信息
    rowData:rowData,
    //autoGridHeight: true,
    root: "data",//服务端返回的数据对象名
    url: basePath + "/fileUpload/list",
    columns: [
        {
            hidden: true,// 是否隐藏该列
            label: "预览图",// 显示列名称
            name: "name",// 列字段属性名
            width: "10",// 列宽度
        },{
            hidden: false,// 是否隐藏该列
            label: "文件名称",// 显示列名称
            name: "name",// 列字段属性名
            width: "80",// 列宽度
        },{
            hidden: false,// 是否隐藏该列
            label: "文件类型",// 显示列名称
            name: "type",// 列字段属性名
            width: "20",// 列宽度
            isOrder: true ,// 是否排序
            formatter: function (columnValue, rowData) {
                if(columnValue===undefined){
                    return '无';
                }else{
                    return columnValue;
                }
            }
        }, {
            hidden: false,// 是否隐藏该列
            label: "文件路径",// 显示列名称
            name: "url",// 列字段属性名
            width: "80"// 列宽度
        },{
            hidden: false,// 是否隐藏该列
            label: "是否缩放",// 显示列名称
            name: "isScale",// 列字段属性名
            width: "30",// 列宽度
            isOrder: true,// 是否排序
            formatter: function (columnValue) {
                if (columnValue == 1) {
                    return "是";
                } else {
                    return "否";
                }
            }
        },{
            hidden: false,// 是否隐藏该列
            label: "上传日期",// 显示列名称
            name: "createTime",// 列字段属性名
            width: "40",// 列宽度
            isOrder: true,// 是否排序
            formatter: function (columnValue) {
                return formatDateTime(columnValue)
            }
        },{
            hidden: false,// 是否隐藏该列
            label: "查看详情",// 显示列名称
            name: "id",// 列字段属性名
            width: "30",// 列宽度
            isFmtHtml: true,//格式化是否返回html
            formatter: function (columnValue, rowData) {
                var btns = [];
                var showFileBtn = $('<a class="griddetailed_btn" href="javascript:">查看</a>');
                var downloadBtn = $('<a class="griddetailed_btn" href="javascript:">下载</a>');
                var deleteBtn = $('<a class="griddetailed_btn" href="javascript:">删除</a>');
                $(downloadBtn).bind("click", function () {
                    mRowData = rowData;
                    downloadFile(rowData.id);
                });
                $(showFileBtn).bind("click", function () {
                    mRowData = rowData;
                    showFile(rowData.id);
                });
                $(deleteBtn).bind("click", function () {
                    deleteFile(rowData.id);
                });
                btns.push(showFileBtn);
                btns.push(downloadBtn);
                btns.push(deleteBtn);
                return btns;
            }
        }
    ],
    event:{
        readyLoad:function(opt,gridObj){
            //grid数据加载前执行
            $(".panel").css("display","none");
        },
        restGridHeight:function(gridHeight,gridObj){
            return gridHeight-$("#searchForm").height();
        }
    }
};

// 附件上传
var uploadStr="";
var uploadDocsPanelOpt = {
    bombType:BOMB_TYPE2,
    isDrag: true,//可拖拽
    haveUpload: true,
    //根据类型动态获取上传url
    uploadParam: {
        uploadUrl: basePath + "/fileUpload/upload?isScale=true"
    },
    valForm: {
        valParam: [//验证参数
            {
                //
            }
        ]
    },//验证
    action: "",//表单提交地址
    event: {
        onClosed: function (obj) {

        },
        okBtnClick: function (eventObj, panel) {

        },
        submitSuccessed: function (data, panelObj) {
            setTimeout(function () {
                panelObj.close();
            }, 1000);
            return;
        },
        onUploadFileQueued: function (file) {
        },
        onUploadSuccessed: function (file, data) {
            var data = JSON.stringify(data);
            uploadStr = uploadStr +data+",";
        },
        uploadComplete: function () {
            // 上传附件赋值
            uploadStr = "";
            uploadDocsPanel.find(".closeBtn").click();
            $("#filesGrid").wdfGrid("reload",filesGridOpt);
        }
    }
};

/***********************面板-end****************************/

/***********************业务操作-start****************************/
//文件上传
function uploadFiles() {
    uploadDocsPanel.show("上传文件");
}

//文件查看
function showFile(id) {
    var showFileUrl = basePath + "/fileUpload/showFile?fileId="+id;
    window.open(showFileUrl);
}

//文件下载
function downloadFile(id) {
    var downloadUrl = basePath + "/fileUpload/downloadFile?fileId="+id;
    window.open(downloadUrl);
}


function deleteFile(id){

    $.wdfProgress.progress("请稍等...",$("body"));
    $.ajax({
        url:basePath+"/fileUpload/deleteFile?fileId="+id,
        dataType:"json",
        type: "post",
        contentType:"application/x-www-form-urlencoded",
        beforeSend:null,
        success:function(data){

            $.wdfProgress.showMsg(data.msg,$("body"),true,function(eventObj){

            });
            if (data.status==1){
                $("#reportGrid").wdfGrid("reload",reportGridOpt);
            }
        },
        error:function (XMLHttpRequest, textStatus, errorThrown) {
            $.wdfProgress.showMsg("系统错误或网络延迟！",$("body"),true);
        }
    });
}

//条件查询
function search(){
    filesGridOpt.param=$("#searchForm").serialize();
    $("#filesGrid").wdfGrid("reload",filesGridOpt);
}

//条件查询重置
function resetSearch() {
    $('#searchForm')[0].reset();
    search();
}
/***********************业务操作-end****************************/

/**************************页面初始化-start*******************************/
$(document).ready(function() {
    filesGridOpt.param=$("#searchForm").serialize();
    filesGridOpt.height = $(document).height() - $('#searchPanel').height()-20;
    $("#filesGrid").wdfGrid("init", filesGridOpt);

    uploadDocsPanel = $("#uploadDocsPanel").wdfPanel("FormPanel", uploadDocsPanelOpt);
});
/**************************页面初始化-end*******************************/